$(function(){
    var fileArr=new Array();
    var fileurlArr = new Array();//上传文件返回的访问url
    var $toast = $('#js_toast');
    var $input = $('#js_input');
    var tmpl = '<li class="weui-uploader__file" style="background-image:url(#url#)"></li>',
        $gallery = $("#gallery"),
        $galleryImg = $("#galleryImg"),
        $uploaderInput = $("#uploaderInput"),
        $uploaderFiles = $("#uploaderFiles");
    $uploaderInput.on("change", function(e) {
        var src, url = window.URL || window.webkitURL || window.mozURL,
            files = e.target.files;
        var length=fileArr.length;
        for(var i = 0, len = files.length; i < len; ++i) {
            var file = files[i];
            if(length + i + 1 > 3) {
                break;
            }
            fileArr.push(file);
            //发送上传图片请求
            uploadUserpic(file);
            if(url) {
                src = url.createObjectURL(file);
            } else {
                src = e.target.result;
            }
            $uploaderFiles.append($(tmpl.replace('#url#', src)));
        }
        checkPhotoSize();
    });

    //控制显示三张以内照片
    function checkPhotoSize(){
        if(fileArr.length>2){
            $(".weui-uploader__input-box").hide();
        }else{
            $(".weui-uploader__input-box").show();
        }
    }

    var index; //第几张图片
    $uploaderFiles.on("click", "li", function() {
        index = $(this).index();
        $galleryImg.attr("style", this.getAttribute("style"));
        $gallery.fadeIn(100);
    });
    $gallery.on("click", function() {
        $gallery.fadeOut(100);
    });
    //删除图片
    $(".weui-gallery__del").click(function() {
        $uploaderFiles.find("li").eq(index).remove();
        fileArr.splice(index,1);
        checkPhotoSize();
    });

    function uploadUserpic(file){
        if(fileArr.length!=0){
            var formData = new FormData();
            formData.append("file",file);
            $.ajax({
                url: "/vote/uploadUserpic",
                type: "POST",
                async: false,
                cache: false,
                processData: false,// 告诉jQuery不要去处理发送的数据
                contentType: false,// 告诉jQuery不要去设置Content-Type请求头
                data: formData,
                success: function(data){
                    console.log(data);
                    console.log(data.data.imgList);
                    fileurlArr.push(data);
                    console.log(fileurlArr);
                    $toast.fadeIn(100);
                    setTimeout(function () {
                        $toast.fadeOut(100);
                    }, 2000);
                }
            });
        }
    }

    $input.on('input', function(){
        if ($input.val()){
            $('#showTooltips').removeClass('weui-btn_disabled');
        }else{
            $('#showTooltips').addClass('weui-btn_disabled');
        }
    });
    $('#showTooltips').on('click', function(){
        if ($(this).hasClass('weui-btn_disabled')) return;
        // toptips的fixed, 如果有`animation`, `position: fixed`不生效
        $('.page.cell').removeClass('slideIn');
        $toast.fadeIn(100);
        setTimeout(function () {
            $toast.fadeOut(100);
        }, 2000);
        //提交表单
        var data = $('form').serialize();
        var content = JSON.stringify(data).replace(/"/gi, '').replace(/&/gi, '<br>');
        console.log("参数："+content);
        $.post('/vote/saveuserinfo', data).error(function(err){
            console.log("====================="+err);
        });
    });
});
